package com.ruoyi.web.controller.catering;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.dto.DishAddDTO;
import com.ruoyi.system.domain.dto.DishMaterialDTO;
import com.ruoyi.system.domain.dto.DishUpdateDTO;
import com.ruoyi.system.domain.vo.DishMaterialVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Dish;
import com.ruoyi.system.service.IDishService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 菜品Controller
 *
 * @author ruoyi
 * @date 2025-03-22
 */
@RestController
@RequestMapping("/system/dish")
public class DishController extends BaseController {
    @Autowired
    private IDishService dishService;

    /**
     * 查询菜品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Dish dish) {
        startPage();
        List<Dish> list = dishService.selectDishList(dish);
        return getDataTable(list);
    }

    /**
     * 查询菜品列表
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(Dish dish) {
        return AjaxResult.success(dishService.selectDishList(dish));
    }


    /**
     * 导出菜品列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dish dish) {
        List<Dish> list = dishService.selectDishList(dish);
        ExcelUtil<Dish> util = new ExcelUtil<Dish>(Dish.class);
        util.exportExcel(response, list, "菜品数据");
    }

    /**
     * 获取菜品详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        DishMaterialVO vo = dishService.getInfo(id);
        return success(vo);
    }

    /**
     * 新增菜品
     */
    @PostMapping
    public AjaxResult add(@RequestBody DishAddDTO dto) {
        return toAjax(dishService.insertDishWithMaterial(dto));
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid DishUpdateDTO dto) {
        dishService.updateDishMaterial(dto);
        return AjaxResult.success("操作成功");
    }

    /**
     * 删除菜品
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(dishService.deleteDishByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<DishMaterialDTO> util = new ExcelUtil<>(DishMaterialDTO.class);
        util.importTemplateExcel(response, "菜品信息");
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<DishMaterialDTO> util = new ExcelUtil<>(DishMaterialDTO.class);
        List<DishMaterialDTO> dishList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        dishService.importDish(dishList, operName);
        return success("数据已全部导入成功！");
    }
}
